package com.souku.finance.application.ui.mine;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.webkit.DownloadListener;
import android.webkit.JavascriptInterface;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import com.android.volley.manager.RequestManager;
import com.souku.finance.application.Manifest;
import com.souku.finance.application.R;
import com.souku.finance.application.base.BaseTitleBarActivity;
import com.souku.finance.application.bean.UserAccMoreBean;
import com.souku.finance.application.http.UrlConstact;
import com.souku.finance.application.http.volley.ReqListener;
import com.souku.finance.application.http.volley.SuccessError;
import com.souku.finance.application.utils.Constant;
import com.souku.finance.application.utils.JsonUtil;
import com.souku.finance.application.utils.ParamUtil;
import com.souku.finance.application.utils.ShareUtil;
import com.souku.finance.application.utils.ToastUtil;
import com.souku.finance.application.utils.level.UIUtils;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;

public class EAccountWebActivity extends BaseTitleBarActivity {

    @BindView(R.id.web_view)
    WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_eaccount_web);
        ButterKnife.bind(this);

        webView.getSettings().setJavaScriptEnabled(true); //设置WebView属性，能够执行Javascript脚本*/
        webView.getSettings().setLightTouchEnabled(true); // 添加一个对象, 让JS可以访问该对象的方法, 该对象中可以调用JS中的方法
        webView.setWebChromeClient(new WebChromeClient());
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);// 当打开新链接时，使用当前的 WebView，不会使用系统其他浏览器
                return true;
            }
        });

        webView.addJavascriptInterface(new AppJavascriptInterface(), "skAppjs");

        String userId = ShareUtil.getInstance().getString(Constant.UserId);//加载需要显示的网页
        String accessToken = ShareUtil.getInstance().getString(Constant.Token);

        String str = "userid=" + userId + "&realname=" + getIntent().getStringExtra("realname") + "&accessToken=" + accessToken
                + "&idCardNumber=" + getIntent().getStringExtra("idCardNumber") + "&appId=APP";

        webView.postUrl(UrlConstact.PAY_BINDING_ACCOUNT, str.getBytes());

    }

    @Override
    public void internet() {

    }

    class AppJavascriptInterface {

        @JavascriptInterface
        public void backApp() {
            getUserAccMore();
        }

    }

    private void getUserAccMore() {
        HashMap<String, String> map = ParamUtil.getParams();
        RequestManager.getInstance().post(UrlConstact.USER_ACC_MORE, ParamUtil.getSing(map), new ReqListener(EAccountWebActivity.this, new SuccessError() {
            @Override
            public void onSuccess(String response) {
                if (JsonUtil.getCode(response) == 1) {
                    UserAccMoreBean bean = JsonUtil.fastBean(JsonUtil.getData(response).toString(), UserAccMoreBean.class);
                    ShareUtil.getInstance()
                            .putExtra(Constant.IsBind, "1")
                            .putExtra(Constant.eAccountNum, bean.geteAccountNum())
                            .putExtra(Constant.realName, bean.getRealName());
                } else {
                    ToastUtil.show(JsonUtil.getMessage(response));
                }
                setResult(300);
                finish();
            }

            @Override
            public void onError(String errorMsg) {
                ToastUtil.show("网络错误");
            }
        }), ParamUtil.getHeaders());
    }
}
